package ar.com.photo_admin.domain

class UserNotification extends DomainObject {

	User notifiedUser
	
	Notification notification
	
	boolean notified = false;
	
	static belongsTo = [notifiedUser:User, notification:Notification]
	
    static constraints = {
    }
		
	static UserNotification link(notifiedUser, notification) {
		def m = UserNotification.findByNotifiedUserAndNotification(notifiedUser, notification)
		if (!m) {
			m = new UserNotification()
			notifiedUser?.addToUserNotifications(m)
			notification?.addToUserNotifications(m)
			m.save()
		}
		
		return m
	}
	
	static void unlink(notifiedUser, notification) {
		def m = UserNotification.findByNotifiedUserAndNotification(notifiedUser, notification)
		if (m) {
			notifiedUser?.removeFromUserNotifications(m)
			notification?.removeFromUserNotifications(m)
			m.delete()
		}
	}
}
